package com.danceforyou.ossFile.controller;

import com.danceforyou.common.model.view.BizData4Page;
import com.danceforyou.common.restful.exception.BizException;
import com.danceforyou.common.restful.exception.BizExceptionEnum;
import com.danceforyou.common.util.AliOssUtil;
import com.danceforyou.common.util.AssertUtil;
import com.danceforyou.common.util.Constants;
import com.danceforyou.common.util.StringUtil;
import com.danceforyou.medium.model.MediumDO;
import com.danceforyou.medium.model.MediumLabelDO;
import com.danceforyou.medium.service.MediumLabelService;
import com.danceforyou.medium.service.MediumService;
import com.danceforyou.medium.service.MediumVoucherService;
import com.danceforyou.medium.vo.MediumLabelVO;
import com.danceforyou.medium.vo.MediumVO;
import com.danceforyou.ossFile.model.OssFileDO;
import com.danceforyou.ossFile.service.OssFileService;
import com.danceforyou.user.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/ossFile")
public class OssFileController {

    @Autowired
    private OssFileService ossFileService;
    @Autowired(required = false)
    private HttpSession session;

    /**
     * 下载云文件
     * @param ossFileId 云文件ID
     * @return boolean
     */
    @RequestMapping(value = "/downloadOssFile", method = RequestMethod.GET)
    public boolean downloadOssFile(Long ossFileId, HttpServletResponse response) {
        UserVO loginUser = (UserVO) session.getAttribute(Constants.SESSION_USER);
        AssertUtil.notNull(loginUser, BizExceptionEnum.AUTHFAILURE);
        OssFileDO ossFile = ossFileService.getById(ossFileId);
        AssertUtil.notNull(ossFile, "云文件不存在");
        byte[] bytes = AliOssUtil.getObject(ossFile.getFileKey());
        AssertUtil.notNull(bytes, "获取文件失败");
        BufferedOutputStream bos = null;
        try {
            response.setContentLength(bytes.length);
            response.addHeader("Content-Disposition", "attachment;filename=" + new String((ossFile.getFileName() +"."+ ossFile.getFileType()).getBytes(), StandardCharsets.ISO_8859_1));
            response.setContentType("application/octet-stream");
            bos = new BufferedOutputStream(response.getOutputStream());
            bos.write(bytes);
            bos.close();
        } catch (IOException e) {
            e.printStackTrace();
            throw new BizException("解析文件失败");
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return true;
    }
}
